package com.cqhilink.iot.jobstandardmanagement.services.innerservice.impl.device;

import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.cqhilink.api.common.utils.enums.RespCodeEnum;
import com.cqhilink.iot.jobstandardmanagement.common.calculator.Time;
import com.cqhilink.iot.jobstandardmanagement.common.calculator.TimeCalc;
import com.cqhilink.iot.jobstandardmanagement.dao.mapper.DeviceValidateCycleMapper;
import com.cqhilink.iot.jobstandardmanagement.facade.model.Device;
import com.cqhilink.iot.jobstandardmanagement.facade.model.DeviceValidateCycle;
import com.cqhilink.iot.jobstandardmanagement.facade.service.device.JsmDeviceValidateCycleService;
import com.cqhilink.iot.jobstandardmanagement.facade.utils.BackMessage;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备校验周期新增
 *
 * @author 黄学乾 778142659@qq.com
 * @create 2017-08-08 11:17
 **/
public class JsmDeviceValidateCycleServiceImpl
        implements JsmDeviceValidateCycleService {
    @Autowired
    protected DeviceValidateCycleMapper cycleMapper;

    /**
     * @param validateCycle 校验周期 内容
     * @return 返回校验周期
     * @throws Exception
     */
    @Override
    public Map<String, Object> insert(DeviceValidateCycle validateCycle) throws Exception {
        return null;
    }

    /**
     * @param validateCycle
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> delete(DeviceValidateCycle validateCycle) throws Exception {
        return null;
    }

    /**
     * @param device 设备操作
     */
    @Override
    public Map<String, Object> operateValidateCycle(Device device) throws Exception {
        Map<String, Object> resMap = new HashMap<>();
        List<Time> ruleList =  new ArrayList<>() ;
        if (device != null) {
            int flag = 0 ;
            // 查看设备的校验周期问题
            if (device.getDeviceValidateCycleList() != null && device.getDeviceValidateCycleList().size() > 0) {
                cycleMapper.deleteDeviceValidateCycle(device);
                List<DeviceValidateCycle> deviceValidateCycles = device.getDeviceValidateCycleList();
                for (DeviceValidateCycle validateCycle : deviceValidateCycles) {
                    if (StringUtils.isEmpty(validateCycle.getValidateCycleId())) {
                        validateCycle.setValidateCycleId(UniqueUtil.uuid());
                        validateCycle.setStatus("0");
                        validateCycle.setDeviceId(device.getDeviceId());
                        flag = flag + cycleMapper.insertSelective(validateCycle);
                    } else {
                        flag = flag + cycleMapper.updateByPrimaryKeySelective(validateCycle);
                    }
                    Time time = new Time();
                    time.setLv(Integer.parseInt(validateCycle.getValidatePv()+""));
                    time.setName(validateCycle.getValidateName());
                    ruleList.add(time);
                }
            }else{
               // 删除了设备校验周期 或者是 修改了设备校验周期
                flag = flag +   cycleMapper.deleteDeviceValidateCycleList(device);
            }
            // 设备校验周期为空
            /*if (device.getDelDeviceValidateCycleList() != null) {
                List<DeviceValidateCycle> delDeviceValidateCycleList = device.getDelDeviceValidateCycleList();
                for (DeviceValidateCycle validateCycle : delDeviceValidateCycleList) {
                    if (StringUtils.isNotEmpty(validateCycle.getValidateCycleId())) {
                        flag = flag + cycleMapper.deleteByPrimaryKey(validateCycle.getValidateCycleId());
                    }
                }
            }*/
            if(flag > 0){
                // 更新更新上次校验时间 和 下次校验时间
                resMap.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
                resMap.put("msg", BackMessage.OPERATE_OK);
                // 更新设备的下一次校验时间
            }
            // 更新设备表中的设备校验时间 和 计算下一次的设备校验时间
            if(ruleList != null && ruleList.size() > 0){
                if(StringUtils.isEmpty(device.getLastValidateTime())){
                }else{
                    // MADE_DATE 出厂日期
                    if(StringUtils.isNotEmpty(device.getInUseTime())){
                        device.setNextValidateTime(
                                DateUtil.DateToString(TimeCalc.calc(device.getInUseTime(),ruleList),
                                        DateStyle.YYYY_MM_DD_HH_MM));
                    }
                }
            }
        } else {
            resMap.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
            resMap.put("msg", "设备为空!");
        }
        return resMap;
    }

    /**
     *
     * @param  deviceId 设备Id
     * @return
     * @throws Exception
     */
    @Override
    public List<DeviceValidateCycle> queryDeviceValidateCycleList(String deviceId) throws Exception {
        return cycleMapper.queryDeviceValidateCycleList(deviceId);
    }
}
